version: 1

script:
- |
  if [ "{{APPIMAGE_SOURCE}}" != "${TARGET_APPDIR}" ]; then
    mv "{{APPIMAGE_SOURCE}}" "${TARGET_APPDIR}"
  fi
  # Manual installation of squashfs-tools is required
  # until https://github.com/AppImageCrafters/build-appimage/issues/5 is fixed
  if ! command -v mksquashfs >/dev/null; then
    apt-get update && apt-get install -y --no-install-recommends squashfs-tools
  fi

AppDir:
  app_info:
    id: org.ryochan7.sc-controller
    name: sc-controller
    version: "{{APPIMAGE_VERSION}}"
    icon: sc-controller
    exec: usr/bin/python3
    exec_args: -c "import os, sys; os.execvp('$APPDIR/usr/bin/scc', ['$APPDIR/usr/bin/scc'] + (sys.argv[1:] if len(sys.argv) > 1 else ['gui']))" $@

  after_bundle: |
    set -eu

    # appimage-builder expects .desktop file to start with appinfo-id
    desktop="$(find "${TARGET_APPDIR}/usr" -name sc-controller.desktop)"
    sed -i "s:Exec=.*:Exec=./usr/bin/scc gui:g" "${desktop}"
    ln -sr "${desktop}" "${TARGET_APPDIR}/usr/share/applications/org.ryochan7.sc-controller.desktop"

    # appimage-builder expects utf-8 encoding when patching shebangs,
    # but pygettext3 has iso-8859-1 encoding
    find "${TARGET_APPDIR}/usr/bin" -name 'pygettext*' | while read -r file; do
      encoding="ISO-8859-1"
      if file -bi "${file}" | grep -iq "${encoding}"; then
        <"${file}" iconv -f "${encoding}" -t utf-8 -o "${file}"
        sed -i -E '1,2 s|^(\s*#.*coding[=:]\s*)([[:alnum:].-]+)|\1utf-8|g' "${file}"
      fi
    done

  apt:
    arch:
      - "{{APPIMAGE_APT_ARCH}}"
    sources:
      - sourceline: deb [arch=amd64] http://archive.ubuntu.com/ubuntu/ {{APPIMAGE_APT_DISTRO}} main universe
        key_url: http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x{{APPIMAGE_APT_PUBKEY}}
      - sourceline: deb [arch=amd64] http://archive.ubuntu.com/ubuntu/ {{APPIMAGE_APT_DISTRO}}-updates main universe
      - sourceline: deb [arch=amd64] http://security.ubuntu.com/ubuntu/ {{APPIMAGE_APT_DISTRO}}-security main universe
      - sourceline: deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports/ {{APPIMAGE_APT_DISTRO}} main universe
      - sourceline: deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports/ {{APPIMAGE_APT_DISTRO}}-updates main universe
      - sourceline: deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports/ {{APPIMAGE_APT_DISTRO}}-security main universe

    include:
      - gir1.2-rsvg-2.0
      - libbluetooth3
      - librsvg2-common
      - python3-evdev
      - python3-gi-cairo
      - python3-pylibacl
      - python3-vdf
      - binutils         # required for detection of bluetooth library
      - coreutils        # provides /usr/bin/env
      - shared-mime-info # required for gui if host provides no MIME info, e.g. when XDG_DATA_DIRS is missing
    exclude:
      - gcc*            # development
      - libgcc*         # development
      - libstdc*        # development
      - libtirpc*       # development
      - libuuid*        # development
      - libattr*        # filesystem
      - libblkid*       # filesystem
      - libmount*       # filesystem
      - libbz*          # codec
      - libjpeg*        # codec
      - liblzma*        # codec
      - libtiff*        # codec
      - libxml*         # codec
      - libwebp*        # codec
      - media-types     # codec
      - libpixman*      # X
      - libxext*        # X
      - libxrender*     # X
      - libfontconfig*  # fonts
      - libfreetype*    # fonts
      - libfribidi*     # i18n
      - libicu*         # i18n
      - libgmp*         # arithmetics
      - libmpdec*       # arithmetics
      - libncurses*     # terminal
      - libreadline*    # terminal
      - readline*       # terminal
      - libssl*         # security
      - "*crypt*"       # security
      - "*krb*"         # security
      - libdb*          # database
      - "*sqlite*"      # database
      - libnsl*         # network


  files:
    exclude:
      - usr/bin/*gold*              # alternative for ld
      - usr/bin/*gp-display-html*   # since Ubuntu Lunatic, requires perl
      - usr/lib/*/gconv             # unicode
      - usr/lib/*/gdk-pixbuf-2.0/*/loaders/libpixbufloader-[!s]*.so # only svg is required
      - usr/lib/*/glib-2.0
      - usr/lib/python*/cgi.py
      - usr/lib/python*/email
      - usr/lib/python*/test
      - usr/lib/python*/unittest
      - usr/share/doc
      - usr/share/glib-2.0
      - usr/share/gtk-doc
      - usr/share/icu
      - usr/share/locale
      - usr/share/man
      - usr/share/python3/runtime.d
      - usr/share/thumbnailers

  runtime:
    env:
      # `usr/lib/python3.*/site-packages` is required in $PYTHONPATH,
      # but the python version and hence the actual location is unknown here.
      # Fortunately the site-packages directory is on the $PATH, so we add $PATH instead.
      # It must precede an existing $PYTHONPATH to work.
      PYTHONPATH: "${APPDIR}/usr/lib/python3/dist-packages:${PATH}:${PYTHONPATH}"
      SCC_SHARED: "${APPDIR}/usr/share/scc"

AppImage:
  arch: "{{APPIMAGE_ARCH}}"
  update-information: "gh-releases-zsync|Ryochan7|sc-controller|latest|sc-controller-*-{{APPIMAGE_APT_DISTRO}}-{{APPIMAGE_ARCH}}.AppImage.zsync"
